<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Http\Model\Goods;
use App\Http\Model\GoodsImage;
use App\Http\Model\ClothingAttr;
use App\Http\Model\OrdersInfo;
use App\Http\Model\Comment;
use DB;

class RecycleController extends Controller
{
	public function recycleBin(){
		$goods = new Goods;
		$goodsInfo = $goods->where("status", "=", "2")->get();
        $goodsInfoHight = $goods->select("goods.*", DB::raw('count(clothing_attr.id) as sum'))->leftJoin("clothing_attr", "goods.id", "=", "goods_id")->groupBy("goods_id")->orderBy("sum", "desc")->first();
		return view("admin.recycle.recycleBin",["goodsInfo"=>$goodsInfo, "goodsInfoHight"=>$goodsInfoHight]);
	}
	public function reply(Request $request){
        $goods_id = $request->input("goods_id");
        $goods = new Goods;
        $nowTime = date("Y-m-d H:i:s");
        $goodsInfo = $goods->find($goods_id);
        $goodsInfo->status = 0;
        $goodsInfo->updated_at = $nowTime;
        $result = $goodsInfo->save();
        if($result){
            echo 1;die;
        }else{
            echo 0;die;
        }
    }
    public function deleteInGrain(Request $request){
    	$goods = new Goods;
        $goodsImage = new GoodsImage;
        $clothingAttr = new ClothingAttr;
        $goods_id = $request->input("goods_id");
        if($goodsInfo = $goods->find($goods_id)){
            if(count($goodsInfo->goodsImage)){
                foreach ($goodsInfo->goodsImage as $k => $v) {
                    @unlink("./upload/goods".$v->url);
                    $goodsImage->destroy($v->id);
                }
                foreach($goodsInfo->clothingAttr as $kk => $vv){
                    $clothingAttr->destroy($vv->id);
                }
            }
            @unlink("./upload/goods".$goodsInfo->pic);
            $goods->destroy($goodsInfo->id);
        }
        echo 1;die;
    }
}